﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Riel_Gestion.dao;
using Riel_Gestion.entities;

namespace Riel_Gestion
{
    public partial class FrmVenta : Form
    {
        String idCliente = null;

        public String IdCliente
        {
            get { return idCliente; }
            set { idCliente = value; }
        }

        public FrmVenta()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {            
            this.Close();
        }

        private void txtImporte_KeyPress(object sender, KeyPressEventArgs e)
        {
            updateTextImporte(txtImporte, e);
        }

        private void txtImportePago_KeyPress(object sender, KeyPressEventArgs e)
        {
            updateTextImporte(txtImportePago, e);
        }

        private void updateTextImporte(TextBox txtBox, KeyPressEventArgs e)
        {
            String text = txtBox.Text.Substring(0, txtBox.Text.Length);
            String aux = "";

            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = true;
                String str = new String(e.KeyChar, 1);
                                           
                aux = text.Substring(0, text.Length - 3);
                aux += text.Substring(text.Length - 2, 1);
                aux += "," + text.Substring(text.Length - 1, 1) + str;

                Double impo = Double.Parse(aux);

                txtBox.Text = "";
                txtBox.Text = String.Format("{0:0.00}", impo);

                txtBox.SelectionStart = txtBox.Text.Length;
            }
            else if (e.KeyChar == '\b')
            {
                e.Handled = true;

                if (text.Length < 4)
                {
                    aux = "0,00";
                }
                else
                {
                    aux = text.Substring(0, text.Length - 4);
                    aux += ",";
                    aux += text.Substring(text.Length - 4, 1);
                    aux += text.Substring(text.Length - 2, 1);
                }

                Double impo = Double.Parse(aux);
                txtBox.Text = "";

                if (impo.ToString().Equals("0"))
                    txtBox.Text = "0,00";
                else
                    txtBox.Text = String.Format("{0:0.00}", impo);

                txtBox.SelectionStart = txtBox.Text.Length;
            }else
                e.Handled = true;

        }

        private void FrmVenta_Load(object sender, EventArgs e)
        {
            txtCliente.Text = idCliente;
            
            DTOSaldo saldo = VentaDAO.getSaldosTotalesById(idCliente);
            txtSaldoAcum.Text = String.Format("{0:0.00}", saldo.TotSaldo);

            
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            DTOSaldo saldoVta = new DTOSaldo(txtCliente.Text);

            saldoVta.TotDebe = long.Parse(txtImporte.Text);
            saldoVta.TotHaber = long.Parse(txtImportePago.Text);
            saldoVta.TotSaldo = saldoVta.TotDebe - saldoVta.TotHaber + long.Parse(txtSaldoAcum.Text);
        }

       
    }
}
